﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmTemperatureList : FrmBase
    {
        private TempratureService tempratureService = new TempratureService();
        private List<Temprature> temperatures = null;

        public FrmTemperatureList()
        {
            InitializeComponent();
            ttlbrhead.Text = "测温管理";
            ilstbcup.Images.Add(ResourceImage.s_增加01);
            ilstbcup.Images.Add(ResourceImage.s_修改01);
            ilstbcup.Images.Add(ResourceImage.s_删除);
            ilstbcup.Images.Add(ResourceImage.s_返回01);

            ilstbcdown.Images.Add(ResourceImage.s_增加02);
            ilstbcdown.Images.Add(ResourceImage.s_修改02);
            ilstbcdown.Images.Add(ResourceImage.s_删除02);
            ilstbcdown.Images.Add(ResourceImage.s_返回02);

            btnPrior.UpImage = ResourceImage.上一页可点击;
            btnPrior.DownImage = ResourceImage.上一页点击;
            btnPrior.DisableImage = ResourceImage.上一页灰;
            btnNext.UpImage = ResourceImage.下一页可点击;
            btnNext.DownImage = ResourceImage.下一页点击;
            btnNext.DisableImage = ResourceImage.下一页灰;
        }

        private void FrmTemperatureList_Load(object sender, EventArgs e)
        {
            temperatures = tempratureService.GetAllTemprature(AppLogic.LoginUser.UserName);

            dlTemperature.DisplayMember = "Title";
            dlTemperature.DataSource = temperatures;
            dlTemperature.DataBind();

            SetButtonState();

        }

        private void SetButtonState()
        {
            btnNext.Enabled = dlTemperature.PageCount > 0 && dlTemperature.PageIndex < dlTemperature.PageCount;
            btnPrior.Enabled = dlTemperature.PageCount > 0 && dlTemperature.PageIndex > 1;
            lblPageInfo.Text = dlTemperature.PageIndex.ToString() + "/" + dlTemperature.PageCount.ToString();
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:  //新增
                    AddTemperature();
                    break;
                case 1:  //修改
                    EditTemperature();
                    break;
                case 2:  //删除
                    DeleteTemperature();
                    break;
                case 3: //返回
                    FormManage.GotoNext("FrmMain", null, null, true);
                    break;
            }
        }

        private void AddTemperature()
        {
            FormManage.GotoNext("FrmTemperatureEdit", new object[] { 0, null }, null, false);
        }

        private void EditTemperature()
        {
            if (dlTemperature.SelectItem == null)
                return;


            Temprature temprature = (Temprature)dlTemperature.SelectItem;
            if (temprature == null)
                return;

            FormManage.GotoNext("FrmTemperatureEdit", new object[] { 1, temprature }, null, false);

        }

        private void DeleteTemperature()
        {
            if (dlTemperature.SelectItem == null)
                return;


            Temprature temprature = (Temprature)dlTemperature.SelectItem;
            if (temprature == null)
                return;

            if (mssdlgSelect.ShowDialog(this, "确认删除当前测温信息？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
            {
                tempratureService.DeleteTemperature(temprature.TempratureID);

                temperatures.Remove(temprature);
                dlTemperature.DataBind();
                SetButtonState();
            }

        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            dlTemperature.PreviousPage();
            SetButtonState();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            dlTemperature.NextPage();
            SetButtonState();
        }
    }
}